/*Calculates 
	- average PDV and dispersion of PDV for entering establishment in manufacturing
	- and manufacturing value added per worker

*/

init_usa // define directories


global median_ElastSubst = 4  
global markupmethod Median    // use change in median markup to calculate profit share of revenue
global lifecap  0  5 10 15    // horizon "0" is the same a using entrants average profits to proxy for PDV 
global deflatortype mfgva     // manufacturing value added deflator
global inputvar iinput pay_total // specify flexible input used to estimate markup variation
global startyear = 1963
global endyear = 2012

/*********************************************************************
	Create discount rate 
*********************************************************************/
run $ProgM/Make_CumDiscountRate.do


/*********************************************************************
	 Calculate real value added per worker
*********************************************************************/
use $DataC/data_clean.dta, replace
destring id, replace

* Merge with price deflators and cumulative discount rates
merge m:1 year using $DataO/deflator_$deflatortype, nogen 
merge m:1 year cohort using $DataO/CumDiscount_constant, keep(match) nogen

bysort year: egen Y = sum(va/deflator_$deflatortype)  // va = gross output - cost of materials
bysort year: egen L = sum(l_total)		      // l_total is total employment

gen ln_YL = ln(Y) - ln(L)

collapse(mean) ln_YL, by(year) fast

save $OutR/YL_$deflatortype, replace


/*********************************************************************
	Calculate profit and interpolate to fill in missing years 
*********************************************************************/

run $ProgM/Make_ProfitShare.do   // calculate profit share of revenue

use $DataC/data_clean.dta, replace
destring id, replace

* Merge with price deflators and cumulative discount rates
merge m:1 year using $DataO/deflator_$deflatortype, nogen   
merge m:1 year cohort using $DataO/CumDiscount_constant, keep(match) nogen  // created by Make_CumDiscountRate.do
merge m:1 year using $OutR/ProfitShare_$markupmethod_$inputvar, nogen       // created by Make_ProfitShare.do 

gen realprofit = y/deflator_$deflatortype*ProfitShare    //y is gross output, ProfitShare created by Make_ProfitShare.do
gen ln_realprofit = ln(realprofit)

keep id year cohort ln_realprofit

xtset id year 

tsfill 

bysort id: ipolate cohort year, gen(temp)

replace cohort = temp

drop temp

bysort id: ipolate ln_realprofit year, gen(temp)
replace ln_realprofit = temp
drop temp
merge m:1 year cohort using $DataO/CumDiscount_constant, keep(match) nogen

gen realprofit = exp(ln_realprofit)

save $DataC/data_yearlyprofit_$deflatortype_$inputvar, replace 



/*********************************************************************
	 Calculate PDV 
*********************************************************************/
use $DataC/data_yearlyprofit_$deflatortype_$inputvar, replace 
destring id, replace


foreach h in $lifecap {

	CalcPDV year cohort id realprofit CumDiscnt, maxage(`h')   // see ado file below
}

clear 



/*********************************************************************
	 Regress ln PDV on ln YL
*********************************************************************/

foreach h in $lifecap {

	

	use $OutR/YL_$deflatortype, replace
	merge 1:1 year using $OutR/PDV_`h'_$inputvar, keep(match) nogen  // created by  CalcPDV

	reg ln_PDV ln_YL

}


/*********************************************************************
	 Regress dispersions of PDV ln PDV on ln YL by cohort
*********************************************************************/

foreach h in $lifecap {

	use $OutR/PDV_microdata_`h', replace  // created by CalcPDV 
	
	drop if cohort == $startyear 		
	gen ln_PDV = log(PDV)  
	bysort cohort: egen disper = sd(ln_PDV) 

	collapse (mean) disper, by(cohort) fast
	
	rename cohort year
	
	merge 1:1 year using $OutR/YL_$deflatortype, keep(match) nogen
 
	reg disper ln_YL
	
	
}



/*********************************************************************
	 Save as ado file
*********************************************************************/
program CalcPDV

	cap program drop CalcPDV
	version 13

	tempvar uniquefirms N_cohort pi_real_disc pi_PDV_cohort 
	
	syntax varlist(min=5 numeric),  MAXAGE(integer)
	tokenize `varlist'
	local yr `1'
	local coh `2'
	local id `3'
	local pi `4'
	local d `5'

	preserve
		
		drop if `coh'>$endyear-`maxage' 	// suppress too new cohorts
		
		gen `pi_real_disc' = `pi'*`d'
		
		replace `pi_real_disc' = 0 if `yr' - `coh' > `maxage'
				
		*** Computing PDV of profit for each firm in a cohort
		bysort `id': egen double PDV =sum(`pi_real_disc')  //summing discounted profit over the life span of each unit
		keep `id' `coh' PDV
		collapse (mean) PDV `coh', by(`id') fast
		
		save $OutR/PDV_microdata_`maxage', replace
		
		*** Computing average PDV of profit by cohort
		* total and average PDV of profit by cohort
		bysort `coh': gen `N_cohort'=_N
		bysort `coh': egen double `pi_PDV_cohort'=sum(PDV)
		gen double ave_PDV_cohort=`pi_PDV_cohort'/`N_cohort'
		
		gen ln_PDV = ln(ave_PDV_cohort)
	
		keep `coh' ln_PDV
	
		collapse (mean) ln_PDV, by(`coh') fast
		
		drop if `coh' == $startyear  

		rename `coh' year
		
		save $OutR/PDV_`maxage'_$inputvar, replace
	restore

end
